package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.LeaseTermService;
import com.atguigu.lease.web.app.service.PaymentTypeService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {


@Autowired
private ApartmentInfoService apartmentInfoService;
@Autowired
private RoomInfoMapper roomInfoMapper;
@Autowired
private GraphInfoMapper graphInfoMapper;
@Autowired
private PaymentTypeService paymentTypeService;

@Autowired
private LeaseTermService leaseTermService;
@Autowired
private LeaseAgreementMapper leaseAgreementMapper;
    @Override
    public List<AgreementItemVo> queryListItem(String phone) {
        List<AgreementItemVo> agreementItemVoList = leaseAgreementMapper.queryListItem(phone);
        return agreementItemVoList;
    }

    @Override
    public AgreementDetailVo customGetDetailById(Long id) {
        //1.判断该id所对应租约信息是否已被删除
        LeaseAgreement leaseAgreement = getById(id);
        if (ObjectUtils.isEmpty(leaseAgreement)){
            return null;
        }

        //2.获取公寓名称
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        String apartmentName = apartmentInfo.getName();
        //3.获取公寓图片列表
        List<GraphVo> graphVoList = graphInfoMapper.queryByapartId2(leaseAgreement.getApartmentId());
        //4.房间号
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        String roomNumber = roomInfo.getRoomNumber();
        //5.房间图片列表
        List<GraphVo> graphVoList1 = graphInfoMapper.queryVoByRoomId(leaseAgreement.getRoomId());
        //6.支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        String paymentTypeName = paymentType.getName();
        //7.租期月数
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        Integer leaseTermMonthCount = leaseTerm.getMonthCount();
        //8.租期单位
        String leaseTermUnit = leaseTerm.getUnit();

        //9.捏到一起
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentName);
        agreementDetailVo.setApartmentGraphVoList(graphVoList);
        agreementDetailVo.setRoomNumber(roomNumber);
        agreementDetailVo.setRoomGraphVoList(graphVoList1);
        agreementDetailVo.setPaymentTypeName(paymentTypeName);
        agreementDetailVo.setLeaseTermMonthCount(leaseTermMonthCount);
        agreementDetailVo.setLeaseTermUnit(leaseTermUnit);
        return agreementDetailVo;
    }
}




